/**
 * notes Array []
 * item {id, title, detail, isBookmark, isDelete, createTime, updateTime}
 */
import dayjs from 'dayjs'
import { v4 as uuidv4 } from 'uuid'

export const clearNoteList = () => {
  return new Promise((resolve, reject) => {
    try {
      window.api.setStoreValue('notes', [])
      resolve()
    } catch (e) {
      return reject(e)
    }
  })
}

export const getAllNoteList = () => {
  return new Promise((resolve, reject) => {
    try {
      let noteList = window.api.getStoreValue('notes') || []
      noteList = noteList.filter((item) => item.isDelete !== 1)
      resolve(noteList)
    } catch (e) {
      return reject(e)
    }
  })
}

export const getNoteList = () => {
  return new Promise((resolve, reject) => {
    try {
      let noteList = window.api.getStoreValue('notes') || []
      noteList = noteList.filter((item) => item.isDelete !== 1)
      resolve(noteList)
    } catch (e) {
      return reject(e)
    }
  })
}

export const getBookmarkList = () => {
  return new Promise((resolve, reject) => {
    try {
      let noteList = window.api.getStoreValue('notes') || []
      noteList = noteList.filter((item) => item.isDelete !== 1 && item.isBookmark === 1)
      resolve(noteList)
    } catch (e) {
      return reject(e)
    }
  })
}

export const getTrashList = () => {
  return new Promise((resolve, reject) => {
    try {
      let noteList = window.api.getStoreValue('notes') || []
      noteList = noteList.filter((item) => item.isDelete === 1)
      resolve(noteList)
    } catch (e) {
      return reject(e)
    }
  })
}

export const fetchBookmark = (noteId, status) => {
  return new Promise((resolve, reject) => {
    try {
      if (!noteId || ![0, 1].includes(status)) {
        return reject(new Error('参数非法'))
      }

      const noteList = window.api.getStoreValue('notes') || []

      const noteIndex = noteList.findIndex((item) => item.id === noteId)
      if (noteIndex === -1) {
        return reject(new Error('非法ID'))
      }
      noteList[noteIndex] = {
        ...noteList[noteIndex],
        isBookmark: status,
        updateTime: dayjs().format('YYYY年MM月DD日 hh:mm:ss')
      }
      window.api.setStoreValue('notes', noteList)
      resolve()
    } catch (e) {
      return reject(e)
    }
  })
}

// 获取详情
export const getNote = (id) => {
  if (!id) {
    return {}
  }
  try {
    const noteList = window.api.getStoreValue('notes') || []
    if (noteList.length) {
      let noteItem = {}
      noteItem = noteList.find((item) => item.id === id)
      return noteItem
    } else {
      return {}
    }
  } catch (e) {
    return []
  }
}

// 保存更新
export const saveNote = (noteItem) => {
  return new Promise((resolve, reject) => {
    try {
      const noteList = window.api.getStoreValue('notes') || []

      let noteId = ''
      if (noteItem.id) {
        noteId = noteItem.id
        const noteIndex = noteList.findIndex((item) => item.id === noteItem.id)
        if (noteIndex === -1) {
          return reject(new Error('非法ID'))
        }
        noteList[noteIndex] = {
          ...noteItem,
          updateTime: dayjs().format('YYYY年MM月DD日 hh:mm:ss')
        }
      } else {
        noteId = uuidv4()
        noteList.push({
          ...noteItem,
          id: noteId,
          createTime: dayjs().format('YYYY年MM月DD日 hh:mm:ss'),
          updateTime: dayjs().format('YYYY年MM月DD日 hh:mm:ss')
        })
      }
      window.api.setStoreValue('notes', noteList)
      resolve(noteId)
    } catch (e) {
      return reject(e)
    }
  })
}

// 移到回收站
export const removeToTrash = (noteId) => {
  return new Promise((resolve, reject) => {
    try {
      if (!noteId) {
        return reject(new Error('参数非法'))
      }

      const noteList = window.api.getStoreValue('notes') || []

      const noteIndex = noteList.findIndex((item) => item.id === noteId)
      if (noteIndex === -1) {
        return reject(new Error('非法ID'))
      }
      noteList[noteIndex] = {
        ...noteList[noteIndex],
        isDelete: 1,
        updateTime: dayjs().format('YYYY年MM月DD日 hh:mm:ss')
      }
      window.api.setStoreValue('notes', noteList)
      resolve()
    } catch (e) {
      return reject(e)
    }
  })
}

export const restoreNote = (noteId) => {
  return new Promise((resolve, reject) => {
    try {
      if (!noteId) {
        return reject(new Error('参数非法'))
      }

      const noteList = window.api.getStoreValue('notes') || []

      const noteIndex = noteList.findIndex((item) => item.id === noteId)
      if (noteIndex === -1) {
        return reject(new Error('非法ID'))
      }
      noteList[noteIndex] = {
        ...noteList[noteIndex],
        isDelete: 0,
        updateTime: dayjs().format('YYYY年MM月DD日 hh:mm:ss')
      }
      window.api.setStoreValue('notes', noteList)
      resolve()
    } catch (e) {
      return reject(e)
    }
  })
}

// 删除
export const delNote = (noteId) => {
  return new Promise((resolve, reject) => {
    try {
      if (!noteId) {
        return reject(new Error('参数非法'))
      }

      const noteList = window.api.getStoreValue('notes') || []

      const noteIndex = noteList.findIndex((item) => item.id === noteId)
      if (noteIndex === -1) {
        return reject(new Error('非法ID'))
      }
      noteList.splice(noteIndex, 1)
      window.api.setStoreValue('notes', noteList)
      resolve()
    } catch (e) {
      return reject(e)
    }
  })
}

// 清空回收站
export const clearTrash = () => {
  return new Promise((resolve, reject) => {
    try {

      let noteList = window.api.getStoreValue('notes') || []
      noteList = noteList.filter((item) => item.isDelete !== 1)

      window.api.setStoreValue('notes', noteList)
      resolve()
    } catch (e) {
      return reject(e)
    }
  })
}
